<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="商品名称" prop="deptName">
            <el-input v-model="queryParams.deptName" placeholder="请输入商品名称" clearable style="width: 200px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item label="商品分类" prop="deptName">
            <el-input v-model="queryParams.deptName" placeholder="请输入商品名称" clearable style="width: 200px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item label="商品层色" prop="deptName">
            <el-input v-model="queryParams.deptName" placeholder="请输入商品名称" clearable style="width: 200px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item label="商品编号" prop="deptName">
            <el-input v-model="queryParams.deptName" placeholder="请输入商品编号" clearable style="width: 200px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item label="商品状态" prop="deptName">
            <el-input v-model="queryParams.deptName" placeholder="请输入商品编号" clearable style="width: 200px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="dataList" row-key="id">
         <el-table-column label="序号" type="index" width="80"></el-table-column>
         <el-table-column prop="name" label="商品名称" min-width="260" show-overflow-tooltip></el-table-column>
         <el-table-column prop="name" label="商品分类" width="260" show-overflow-tooltip>
            <template #default="scope">
               {{ `${scope.row.firstClassifyName}/${scope.row.secondClassifyName}` }}
            </template>
         </el-table-column>
         <el-table-column prop="sellPrice" label="销售价格" width="100" show-overflow-tooltip>
            <template #default="scope">
               {{   `${(scope.row.sellPrice/100).toFixed(2)}` }}
            </template>
         </el-table-column>
         <el-table-column prop="" label="收藏" width="100" show-overflow-tooltip></el-table-column>
         <el-table-column prop="useDeg" label="层色" width="100" show-overflow-tooltip>
            <template #default="scope">
            {{   `${(scope.row.useDeg/100).toFixed(2)}` }}
            </template>
         </el-table-column>
         <el-table-column label="状态" width="100" show-overflow-tooltip>
            <template #default="scope">
               {{ scope.row.goodsStatus === 0 ? '下架' : '上架' }}
            </template>
         </el-table-column>
         <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280">
            <template #default="scope">
               <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
               <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row,'view')">查看</el-button>
               <el-button link type="primary" icon="Edit" @click="handlePutaway(scope.row)">{{ scope.row.goodsStatus === 0 ?
                  '上架' : '下架' }}</el-button>
               <!-- <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button> -->
            </template>
         </el-table-column>
      </el-table>
      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />    
      <!-- <CreateGoodsLabel v-if="isCreateHide" :isHide="isCreateHide" @update:isUpdateCreateHide="ev => isCreateHide = ev"
         :dataInfo="dataInfo" :titleName="titleName" @successAction="getList()" /> -->
   </div>
</template>
 
<script setup>

import {
   GetGoodsList, // 商品列表查询
   UpDownGoods, // 商品上架下架
} from "@/api/goodsManage/goods"
import { onMounted } from "vue";
import { ElMessageBox } from 'element-plus'

// import CreateGoodsLabel from "./createGoodsLabel.vue"
import { useRoute, useRouter } from 'vue-router'
let router = useRouter()

const dataList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
let dataInfo = ref({})

let isCreateHide = ref(false)
let titleName = ref("新增")

const data = reactive({
   queryParams: {
      deptName: "",
      status: "",
      pageNum: 1,
      pageSize: 10,
   },
});

let total = ref(0)

const { queryParams, rules } = toRefs(data);

/** 查询商品列表 */
const getList = async () => {
   loading.value = true;
   try {
      let data = await GetGoodsList(queryParams.value)
      dataList.value = data.rows
    total.value = data.total
      loading.value = false;
   } catch (error) {
      loading.value = false;
   }

}

/** 搜索按钮操作 */
const handleQuery = () => {
   getList();
}
/** 重置按钮操作 */
let queryRef = ref(null)
const resetQuery = () => {
   queryRef.value.resetFields();
   handleQuery();
}
/** 新增按钮操作 */
const handleAdd = (row) => {
   router.push({
      name:"createEditGoodsDetails"
   })
}

/** 修改按钮操作 */
const handleUpdate = (row,types) => {
   router.push({
      name:"createEditGoodsDetails",
      query:{
         id:row.id,
         view:types ? types : ""
      }
   })
}

/** 上架下架按钮操作 */
const handlePutaway = (row) => {
   let params = {
      id:row.id,
      goodsStatus:row.goodsStatus === 0 ? 1 : 0
   }
   let strStatus = row.goodsStatus === 0 ? '上架' : '下架'
   ElMessageBox.confirm('是否确认修改名称为"' + row.name + '"的商品上架状态为'+ strStatus +'?').then(() => {
      return UpDownGoods(params);
   }).then(() => {
      getList();
      ElMessageBox.msgSuccess("删除成功");
   }).catch(() => { });
}

onMounted(() => {
   getList();
})
</script>
 